/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sun.courier.view;

import com.sun.courier.DemoProperties;
import com.sun.courier.dal.CourierDAL;
import com.sun.courier.entity.Courier;
import com.sun.courier.model.ButtonsRenderer;
import com.sun.courier.model.CourierModel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

/**
 *
 * @author quanbh_c00268
 */
@DemoProperties(
        value = "Courier",
        category = "Courier Trace",
        description = "Magage Courier Information, Including: Insert, Update and Delete",
        sourceFiles = { //                "com/sun/courier/view/EmployeeManagement.java",
        //                "com/sun/courier/view/resources/images/EmployeeManagement.png",
        }
)
public class CourierManagement extends JPanel implements ActionListener{

    DefaultTableModel model = null;
    JPanel controlPanel;
    JTextField txtCourierName;
    JTextField filterField;
    JTextArea output;
    CourierDAL courierDal = new CourierDAL();

    public JTable courierTable;
    
    public CourierManagement() throws Exception {
        initComponents();
    }
    
    public void initComponents() throws Exception {
        setLayout(new BorderLayout());
        controlPanel = createControlPanel();
        add(controlPanel, BorderLayout.NORTH);
        createJTable();
    }

    protected JPanel createControlPanel() {
        JPanel controlPanel = new JPanel();
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        controlPanel.setLayout(gridbag);

        c.gridx = 0;
        c.gridy = 1;
        c.gridheight = 1;
        c.insets = new Insets(20, 10, 0, 10);
        c.anchor = GridBagConstraints.SOUTHWEST;
        JLabel searchLabel = new JLabel("Search Titles and Recipients");
        controlPanel.add(searchLabel, c);

        c.gridx = 0;
        c.gridy = 2;
        c.weightx = 1.0;
        c.insets.top = 0;
        c.insets.bottom = 12;
        c.anchor = GridBagConstraints.SOUTHWEST;
        //c.fill = GridBagConstraints.HORIZONTAL;
        filterField = new JTextField(24);
//        filterField.getDocument().addDocumentListener(new TableDemo.SearchFilterListener());
        controlPanel.add(filterField, c);

        c.gridx = 1;
        c.gridy = 2;
        c.gridwidth = GridBagConstraints.REMAINDER;

        return controlPanel;
    }

    public void createJTable() {
        courierTable = makeTable();
//        table.setColumnModel(createColumnModel());
        courierTable.setAutoCreateRowSorter(true);
        courierTable.setRowHeight(30);
        courierTable.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
        courierTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        courierTable.setIntercellSpacing(new Dimension(0, 0));

        Dimension viewSize = new Dimension();
        viewSize.width = courierTable.getColumnModel().getTotalColumnWidth();
        viewSize.height = 10 * courierTable.getRowHeight();
        courierTable.setPreferredScrollableViewportSize(viewSize);
//        </snip> Right click on JTable

        //<snip>
        JTableHeader header = courierTable.getTableHeader();
        header.setPreferredSize(new Dimension(30, 26));
        TableCellRenderer headerRenderer = header.getDefaultRenderer();
        if (headerRenderer instanceof JLabel) {
            ((JLabel) headerRenderer).setHorizontalAlignment(JLabel.CENTER);
        }

        JScrollPane scrollpane = new JScrollPane(courierTable);
        add(scrollpane, BorderLayout.CENTER);
    }

    public JTable makeTable() {
        String empty = "";
        String[] columnNames = {"Courier Id", "Courier Name", "Customer Name", "Phone", "Status", "Weight", "Departed Location", "Destination", "Distance", "Actions"};
        Object[][] data = null;
        try {
            ArrayList<Courier> lst = courierDal.loadAllCourier();
            data = new Object[lst.size()][10];
            for (int i = 0; i < lst.size(); i++) {
                data[i][0] = lst.get(i).getCourierId();
                data[i][1] = lst.get(i).getCourierName();
                data[i][2] = lst.get(i).getCustomerName();
                data[i][3] = lst.get(i).getPhone();
                data[i][4] = lst.get(i).getStatus();
                data[i][5] = lst.get(i).getWeight();
                data[i][6] = lst.get(i).getDepartLocation();
                data[i][7] = lst.get(i).getDestination();
                data[i][8] = lst.get(i).getDistance();
                data[i][9] = empty;
            }
        } catch (Exception ex) {
            Logger.getLogger(CourierManagement.class.getName()).log(Level.SEVERE, null, ex);
        }

        model = new DefaultTableModel(data, columnNames) {
            @Override
            public Class<?> getColumnClass(int column) {
                return getValueAt(0, column).getClass();
            }
        };
        
        final JTable table = new JTable(model);
//        table.setRowHeight(26);
        table.setAutoCreateRowSorter(true);
        //table.addMouseListener(new CellButtonsMouseListener());
        //ButtonsEditorRenderer er = new ButtonsEditorRenderer(table);
        TableColumn column = table.getColumnModel().getColumn(9);//CHU Y CHO NAY DE TAO JBUTTON
        TableColumn column1 = table.getColumnModel().getColumn(0);
        column1.setMaxWidth(0);
        column1.setMinWidth(0);
        column1.setWidth(0);
        column1.setPreferredWidth(0);
        column.setCellRenderer(new ButtonsRenderer());
        return table;
    }

    public static void main(String[] args) throws Exception {
        JFrame f = new JFrame();
        CourierManagement c = new CourierManagement();
        f.add(c);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(new Dimension(700, 400));
        f.setVisible(true);
    }
    
    public void actionPerformed(ActionEvent ae) {
        
    }
    
    private void insertNewRecord() throws Exception {
        int row = -1;
        CourierActions ca = null;
        if(ca == null){
            ca = new CourierActions(model, row);
            ca.setAlwaysOnTop(true);
            ca.setSize(new Dimension(600, 500));
            ca.setVisible(true);
        }else{
            ca.setSize(new Dimension(600, 500));
            ca.invalidate();
            ca.validate();
            ca.repaint();
            ca.setAlwaysOnTop(true);
            ca.setVisible(true);
        }
        
    }
}
